package com.aglhz.nature.presenter;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;


import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.ShopListViewEvent;
import com.aglhz.nature.modle.ShopListBean;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.ShopListView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

public class ShopListPresenter extends BasePresenter{
	private static String TAG = ShopListPresenter.class.getName();
	private int appType=2;
	// view
	private ShopListView shopListView;
	// data
	private ShopListBean shopListBean;
	private List<ShopListData> data;
	public ShopListPresenter(ShopListView view) {
		super();
		shopListView = view;
	}
	
	// /*********************data operation*******************///
			public void loadData() throws Exception {
				// load remote data
				String categoryId=shopListView.getCategoryId();
				String brandId=shopListView.getBrandId();
				String name=shopListView.getName();
				String categoryTemplateId=shopListView.getCategoryTemplateId();
				String editAround=shopListView.getAroundInfo();
				
				
				this.loadShopListBean( brandId, editAround,name);
				// save remote data
				
			}
			
			// /*********************view operation*******************///
			private void showListView() {
				shopListView.showList(shopListBean.getData());
				
			}
			
			
			///********************view operation*******************///
			@Subscribe(threadMode = ThreadMode.MAIN)
			public void onEventShopListThread(ShopListViewEvent event) {

				String msg = event.getMessage();
				Log.i(TAG, " ShopListViewEvent:" + msg);
				// update UI By event
				if ( ShopListViewEvent.EVENT_UPDATE_SHOP_LISTVIEW == msg) {
					// update grid view
					this.showListView();
				} 
				

			}
			
			
			
			
			
			
			
			

			private void loadShopListBean(String brandId,String editAround,String name ) throws Exception {
				if(!(null==brandId)){
					String url = ServerAPI.APIHEARD+ServerAPI.SEARCH+"?brandId="+brandId;
					
					Log.e("---------url---",url);
					AsyncHttpClient client = new AsyncHttpClient();
					client.post(url, new TextHttpResponseHandler() {
						;
						@Override
						public void onSuccess(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString) {
							 try {
								 Log.e(TAG, "response" + responseString);
								
								 Gson gson = new Gson();
								
								 ShopListBean slBean = gson.fromJson(responseString,
										 ShopListBean.class);
								 shopListBean= (ShopListBean) slBean.clone();
								
								Log.d(TAG,"ShopListView---111-----"+shopListBean.getOther());	
								data = shopListBean.getData();
								Log.d(TAG,"ShopListView---mcp-----"+shopListBean.getData());	
								 // send event
								ShopListViewEvent event = new   ShopListViewEvent(
										ShopListViewEvent.EVENT_UPDATE_SHOP_LISTVIEW);
								 EventBus.getDefault().post(event);
								 
								 Log.e(TAG, "success" + "success");
								 } catch (Exception e) {
								 e.printStackTrace();
								 }
							
							
						}
						
						@Override
						public void onFailure(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString, Throwable throwable) {
							
							 Log.e(TAG, "failure");
						}
					

				

				
					});
					//周边搜索
				}else if(!(null==editAround)){
					String url = ServerAPI.APIHEARD+ServerAPI.HOME_AROUND;
					 RequestParams params = new RequestParams();  
					    params.put("keyword", editAround);
					Log.e("---------url---",url);
					AsyncHttpClient client = new AsyncHttpClient();
					client.post(url,params, new TextHttpResponseHandler() {
						
						@Override
						public void onSuccess(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString) {
							 try {
								 Log.e(TAG, "response" + responseString);
								
								 Gson gson = new Gson();
								
								 ShopListBean slBean = gson.fromJson(responseString,
										 ShopListBean.class);
								 shopListBean= (ShopListBean) slBean.clone();
								
								Log.d(TAG,"ShopListView---111-----"+shopListBean.getOther());	
								data = shopListBean.getData();
								Log.d(TAG,"ShopListView---mcp-----"+shopListBean.getData());	
								 // send event
								ShopListViewEvent event = new   ShopListViewEvent(
										ShopListViewEvent.EVENT_UPDATE_SHOP_LISTVIEW);
								 EventBus.getDefault().post(event);
								 
								 Log.e(TAG, "success" + "success");
								 } catch (Exception e) {
								 e.printStackTrace();
								 }
							
							
						}
						
						@Override
						public void onFailure(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString, Throwable throwable) {
							
							 Log.e(TAG, "failure");
						}
					

				

				
					});
					
					//搜索
				}else if(!(null==name)){
					String url = ServerAPI.APIHEARD+ServerAPI.SEARCH+"?name="+name;
					
					Log.e("---------url---",url);
					AsyncHttpClient client = new AsyncHttpClient();
					client.post(url, new TextHttpResponseHandler() {
						;
						@Override
						public void onSuccess(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString) {
							 try {
								 Log.e(TAG, "response" + responseString);
								
								 Gson gson = new Gson();
								
								 ShopListBean slBean = gson.fromJson(responseString,
										 ShopListBean.class);
								 shopListBean= (ShopListBean) slBean.clone();
								
								Log.d(TAG,"ShopListView---111-----"+shopListBean.getOther());	
								data = shopListBean.getData();
								Log.d(TAG,"ShopListView---mcp-----"+shopListBean.getData());	
								 // send event
								ShopListViewEvent event = new   ShopListViewEvent(
										ShopListViewEvent.EVENT_UPDATE_SHOP_LISTVIEW);
								 EventBus.getDefault().post(event);
								 
								 Log.e(TAG, "success" + "success");
								 } catch (Exception e) {
								 e.printStackTrace();
								 }
							
							
						}
						
						@Override
						public void onFailure(int statusCode,
								cz.msebera.android.httpclient.Header[] headers,
								String responseString, Throwable throwable) {
							
							 Log.e(TAG, "failure");
						}
					

				

				
					});
				}
			

				

				
			}		
		}


